package com.ydh.company.controller;

import com.ydh.company.dto.ResponseData;
import com.ydh.company.dto.ResultData;
import com.ydh.company.pojo.Client;
import com.ydh.company.pojo.Condition;
import com.ydh.company.pojo.Repository;
import com.ydh.company.utils.UUIDUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("repository")
@Api(value = "仓库管理接口",tags = "仓库管理接口API（6个）HKill")
public class RepositoryController {


    @PostMapping(value = "/queryRepositoryByPage")
    @ResponseBody
    @ApiOperation(value = "1仓库列表分页查询接口", notes = "展示初始列表信息", httpMethod = "POST", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败")}
    )
    public ResultData queryRepositoryByPage(@RequestBody @ApiParam(name = "Condition", value = "传入页码和页容量", required = true) Condition condition) {

        Map<String,Object> resultMap = new HashMap<>();

        //获取当前页码
        int currentPage = condition.getCurrentPage();
        //获取页容量
        int perSize = condition.getPerSize();
        System.out.println("当前页码：" + currentPage);
        System.out.println("页容量：" + perSize);

        List<Repository> repositoryList = new ArrayList<>();

        Repository repository1 = new Repository();
        repository1.setRepositoryId(UUIDUtil.getId());
        repository1.setRepositoryName("深圳仓");
        repository1.setRepositoryCode("001");
        repository1.setRepositoryAddress("广东深圳");
        repository1.setRepositoryStatus(1);
        repository1.setFlag(1);

        Repository repository2 = new Repository();
        repository2.setRepositoryId(UUIDUtil.getId());
        repository2.setRepositoryName("成都仓");
        repository2.setRepositoryCode("002");
        repository2.setRepositoryAddress("四川成都");
        repository2.setRepositoryStatus(0);
        repository2.setFlag(0);

        repositoryList.add(repository1);
        repositoryList.add(repository2);


        resultMap.put("totalCounts",35);
        resultMap.put("clientList", repositoryList);

        return ResponseData.success(resultMap);
    }







    @PostMapping(value = "/addRepository")
    @ResponseBody
    @ApiOperation(value = "2新增仓库接口",notes = "把仓库信息存放在数据库中",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData addRepository(@RequestBody @ApiParam(name = "Repository",value = "新增仓库信息",required = true) Repository repository){

        String repositoryId = UUIDUtil.getId(); //不用传
        String repositoryName = repository.getRepositoryName();
        String repositoryCode = repository.getRepositoryCode();
        String repositoryAddress = repository.getRepositoryAddress();
        System.out.println("id" + repositoryId);
        System.out.println("仓库名称" + repositoryName);
        System.out.println("仓库编码" + repositoryCode);
        System.out.println("仓库地址" + repositoryAddress);

        int count = 1;
        if (count == 1){
            return ResponseData.success(count);
        }else {
            return ResponseData.error(400,"新增失败");
        }

    }

    @PostMapping(value = "/updateRepository")
    @ResponseBody
    @ApiOperation(value = "3修改仓库信息接口",notes = "对仓库信息进行修改",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData updateRepository(@RequestBody @ApiParam(name = "Repository",value = "通过仓库id（编码）,修改仓库名称和地址",required = true) Repository repository){

        String repositoryId = repository.getRepositoryId();
        String repositoryName = repository.getRepositoryName();
        String repositoryCode = repository.getRepositoryCode();
        String repositoryAddress = repository.getRepositoryAddress();
        System.out.println("仓库id（编码）" + repositoryId);
        System.out.println("仓库名称" + repositoryName);
        System.out.println("仓库编码" + repositoryCode);
        System.out.println("仓库地址" + repositoryAddress);

        int count = 1;
        if (count == 1){
            return ResponseData.success(count);
        }else {
            return ResponseData.error(400,"修改失败");
        }

    }


    @PostMapping(value = "/prohibitRepository")
    @ResponseBody
    @ApiOperation(value = "4禁用仓库接口",notes = "设置仓库状态为禁用",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData prohibitRepository(@RequestBody @ApiParam(name = "Repository",value = "通过仓库id,禁用仓库",required = true) Repository repository){

        String repositoryId = repository.getRepositoryId();
        System.out.println("仓库id" + repositoryId);

        int count = 1;
        if (count == 1){
            return ResponseData.success(count);
        }else {
            return ResponseData.error(400,"禁用失败");
        }
    }


    @PostMapping(value = "/judgeRepositoryHasClient")
    @ResponseBody
    @ApiOperation(value = "5判断是否有客户在使用此仓库接口",notes = "判断是否有客户在使用此仓库",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData judgeClientLevelHasClient(@RequestBody @ApiParam(name = "Client",value = "传仓库id，进行判断",required = true) Client client){

        String repositoryId = client.getRepositoryId();
        System.out.println("仓库id：" + repositoryId);

        int count = 0;
        if (count == 0){
            return ResponseData.success("允许删除！");
        }else {
            return ResponseData.error(400,"此仓库有客户使用，不允许删除！");
        }
    }



    @PostMapping(value = "/delRepository")
    @ResponseBody
    @ApiOperation(value = "6删除仓库信息接口",notes = "删除仓库信息",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData delRepository(@RequestBody @ApiParam(name = "Repository",value = "通过仓库id,删除仓库信息",required = true) Repository repository){

        String repositoryId = repository.getRepositoryId();
        System.out.println("仓库id" + repositoryId);

        int count = 1;
        if (count == 1){
            return ResponseData.success(count);
        }else {
            return ResponseData.error(400,"删除失败");
        }
    }


}
